package com.atguigu.rabbitmq.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/*TTL队列 配置文件类代码*/
@Configuration
public class TtlQueueConfig {
    //普通交换机名称
    public static final  String X_EXCHANGE="X";
    //死信交换机名称
    public static final  String Y_DEAD_LETTER_EXCHANGE="Y";
    //普通队列名称
    public static final  String QUEUE_A="QA";
    public static final  String QUEUE_B="QB";
    //死信队列名称
    public static final  String DEAD_LETTER_QUEUE="QD";

    //普通队列的名称(这个队列是通用的延迟队列，没有固定的延迟多少秒，由发消息的人决定延迟多少秒)
    public static final  String QUEUE_C="QC";

    //声明QC
    @Bean("queueC")
    public Queue queueC(){
        Map<String, Object> arguments=new HashMap<>(3);//3是map的长度
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //不要设置延迟时间
        return QueueBuilder.durable(QUEUE_C).withArguments(arguments).build();
    }
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,@Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }

    //声明x交换机
    @Bean("xExchange")//括号里是起的别名
    public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    //声明y交换机
    @Bean("yExchange")//括号里是起的别名
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    //声明队列 要有TTL(过期时间)为10秒
    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> arguments=new HashMap<>(3);//3是map的长度
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置TTL
        arguments.put("x-message-ttl",10000);

        return QueueBuilder.durable(QUEUE_A).withArguments(arguments).build();
    }

    //声明队列 要有TTL(过期时间)为40秒
    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> arguments=new HashMap<>(3);//3是map的长度
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",Y_DEAD_LETTER_EXCHANGE);
        //设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key","YD");
        //设置TTL
        arguments.put("x-message-ttl",40000);

        return QueueBuilder.durable(QUEUE_B).withArguments(arguments).build();
    }

    //死信队列
    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }

    //绑定(x交换机和队列A)
    @Bean
    public Binding queueABindingX(@Qualifier("queueA")Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){//@Qualifier注解可以结合别名来绑定
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }
    //绑定(x交换机和队列B)
    @Bean
    public Binding queueBBindingX(@Qualifier("queueB")Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){//@Qualifier注解可以结合别名来绑定
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }
    //绑定(y交换机和队列D)
    @Bean
    public Binding queueDBindingY(@Qualifier("queueD")Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){//@Qualifier注解可以结合别名来绑定
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }


}
